<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>购买</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.min.css" />
    <link rel="stylesheet" type="text/css" href="./css/styleold.css" />
    <script type="text/javascript" src="./script/api.js"></script>
    <script type="text/javascript" src="./script/aui-tab.js"></script>
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <script type="text/javascript" src="./script/aui-slide.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
    <script type="text/javascript">
        function closeWin() {
            api.closeWin({});
        };
    </script>
</head>

<body>
    <!-- 头部 -->
    <header class="aui-bar aui-bar-nav pos">
        <a class="aui-pull-left aui-btn" onclick="closeWin()">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title">购买</div>
    </header>
    <div class="header-d"></div>
    <style>
        body,
        html {
            min-height:calc(100% - 0);
        }
    </style>
    <div class="licai-buy">
        <dl>
            <dt>投资金额：</dt>
            <dd>
                <h1 onclick="reducemoney();">-</h1><i>￥</i><span id="money"></span>
                <h1 onclick="addmoney();">+</h1></dd>
            </dl>
            <dl>
                <dt></dt>
                <dd>
                    <b class="active" onclick="sel_times(this);">X1</b>
                    <b onclick="sel_times(this);">X5</b>
                    <b onclick="sel_times(this);">X10</b>
                </dd>
            </dl>

            <!-- <dl>
                <dt>使用红包：</dt>
                <dd>
                    <div class="aui-list-item-inner">
                        <div id="hongbaoList"></div>
                        <span>超出部分不返还</span>
                    </div>
                </dd>
            </dl> -->
            <!-- <dl>
                <dt>加息券：</dt>
                <dd>
                    <div class="aui-list-item-inner">
                        <div id="jiaxijuanList"></div>
                    </div>
                </dd>
            </dl> -->
            <dl>
                <dt>获利方式：</dt>
                <dd>
                    <div class="aui-list-item-inner">
                        <label><input class="aui-radio" type="radio" name="huolifangshi" checked value="1"> 每日获利</label>
                        <!-- <label><input class="aui-radio" type="radio" name="huolifangshi" onclick="selHuolifangshi(this.value);" value="2"> 分期获利</label> -->
                    </div>
                </dd>
            </dl>
        <!-- <dl>
            <dt>支付密码：</dt>
            <dd>
                <input type="password">
            </dd>
        </dl> -->
        <dl>
            <dt>预期收益：</dt>
            <dd id="shouyi"></dd>
        </dl>
        <dl>
            <dt>实付金额：</dt>
            <dd id="shifu"></dd>
        </dl>
        <div class="licai-buy-d">
            <div class="aui-clearfix"></div>
            <span onclick="xieyi1();">《借款协议》</span>
            <span onclick="xieyi2();">《委托授权书》</span>
            <span onclick="xieyi3();">《风险揭示书》</span>
            <span onclick="xieyi4();">《金可贷--电子签名数字证书用户申请确认函》</span>
        </div>
    </div>
    <div class="licai-buy-btn">
        <div class="aui-col-xs-6" onclick="buy();">购买</div>
        <div class="aui-col-xs-6" onclick="cal();">取消</div>
    </div>
</body>

<script type="text/javascript">
    var true_money = '';
    var ex_rate = 0;
    var free_money = 0;
    var max_money = 0;
    var times = 1;

    apiready = function() {

        min_money_format_num = api.pageParam.min_money_format_num;
        rate = api.pageParam.rate;
        id = api.pageParam.id;
        max_money = api.pageParam.max_money;
        add_money = api.pageParam.add_money;

        true_money = min_money_format_num;

        $('#money').text(outputmoney(min_money_format_num));

        changeShouyi(min_money_format_num);
        changeShifu();
    }

    function sel_times(i) {
        $("b").each(function(){
            $(this).removeClass("active");
        });
        $(i).addClass("active");
        times = $(i).text().substring(1);
    }

    function changeShouyi(money) {

        var radio = document.getElementsByName('huolifangshi');
        for (i = 0; i < radio.length; i++) {
            if (radio[i].checked) {
                var type = radio[i].value;
            }
        }

        all_rate = parseFloat(rate) + parseFloat(ex_rate);


        $('#shouyi').text('￥' + outputmoney(parseFloat(money) * parseFloat(parseFloat(all_rate).toFixed(2)) / 100 / 365));


    }

    function changeShifu() {

        $('#shifu').text('￥' + outputmoney(parseInt(true_money) - parseInt(free_money)));
    }

    function addmoney() {

        var money = parseInt($('#money').text().replace(/,/g, ''));

        if (money + add_money * times > max_money) {
            alert('最高可投金额为' + max_money + '元');
            $('#money').text(outputmoney(max_money));
            true_money = max_money;
            changeShouyi(max_money);
            changeShifu();
            return;
        }

        money = money + add_money * times;
        $('#money').text(outputmoney(money));
        true_money = money;
        changeShouyi(money);
        changeShifu();
    }

    function reducemoney() {
        var money = parseInt($('#money').text().replace(/,/g, ''));
        if (min_money_format_num >= money) {
            alert('最低可投金额为' + outputmoney(min_money_format_num) + '元');
            $('#money').text(outputmoney(min_money_format_num));
            true_money = min_money_format_num;
            changeShouyi(min_money_format_num);
            changeShifu();
            return;
        }

        if (money > min_money_format_num * times) {
            money = money - add_money * times;
            $('#money').text(outputmoney(money));
            true_money = money;
            changeShouyi(money);
            changeShifu();
        }else {
            alert('最低可投金额为' + outputmoney(min_money_format_num) + '元');
            $('#money').text(outputmoney(min_money_format_num));
            true_money = min_money_format_num;
            changeShouyi(min_money_format_num);
            changeShifu();
            return;
        }
    }

    function cal() {
        api.closeWin({});
    }

    function xieyi1() {

    }

    function xieyi2() {

    }

    function xieyi3() {

    }

    function xieyi4() {

    }

    function buy() {

      if (api.systemType == 'ios') {
          var password = $api.getStorage('password');
          if (password) {
            api.openWin({
                name: 'ios-unlockBuy',
                url: './ios-unlockBuy.html',
                bgColor:'#00744b',
                slidBackEnabled:false,
                pageParam:{
                  login:'licai-buy-yuebao'
              },
          });

        } else {
          alert('请先添加手势密码');
      }

  } else {
      var isLock = $api.getStorage('isLock');
      if (isLock) {
          var screenLock = api.require('screenLock');
          screenLock.show({
              color: '#00744b'
          }, function(ret, err) {
              $api.setStorage('buy', true);
              if (ret.status == '111') {
                  api.showProgress({
                      style: 'default',
                      animationType: 'fade',
                      title: '努力加载中...',
                      text: '先喝杯茶...',
                      modal: false
                  });
                  pay();
              }
          });
      } else {
          alert('请先添加手势密码');
      }
  }
}
function reload() {

  api.closeWin({
      name: 'ios-unlockBuy'
  });
  api.toast({
      msg: '解锁失败',
      duration: 2000,
      location: 'middle'
  });

  setTimeout("opeanReload()", 400);


}

function opeanReload() {
  api.openWin({
      name: 'ios-unlockBuy',
      url: './ios-unlockBuy.html',
      bgColor:'#00744b',
      slidBackEnabled:false,
      pageParam:{
        login:'licai-buy-yuebao'
    },
});

}


function pay() {
    var userid = $api.getStorage('userid');
        // 5154


        url = webAddress + '/index.php?ctl=deal&act=dobid2';
        api.ajax({
            url: url,
            method: 'post',
            data: {
                values: {
                    ajax: '1',
                    agent: 'h5',
                    userid: userid,
                    id: id,
                    money: true_money,
                    hl_type: 2,
                }
            }
        }, function(ret, err) {
            if (ret) {


                if (ret.status == 0) {

                    alert(ret.info);
                } else {
                    api.ajax({
                        url: webAddress + ret.jump,
                        method: 'get',
                        data: {
                            values: {
                                userid: userid,
                            }
                        }
                    }, function(ret, err) {
                        if (ret) {

                            if (ret.status == 0) {
                                alert(ret.info);
                                return;
                            }

                            api.toast({
                                msg: '购买成功',
                                duration: 2000,
                                location: 'middle'
                            });

                            api.openWin({
                                name: 'zhitou-list',
                                url: './zhitou-list.html',
                                pageParam: {
                                    toubiao: 'toubiao'
                                }
                            });

                        } else {
                            alert(err.msg);
                        }
                    });
                }
            } else {
                alert(err.msg);
            }
        });
        api.hideProgress();
    }
</script>

</html>
